import {AnyObj} from "../@types/index";
/**
 * 过滤空值对象
 * @param data , 需要过滤的数据
 * @param filterFn
 */
export const filterObject = (
    data:AnyObj = {} ,
    filterFn = (key:any) =>
        data[key] !== null && data[key] !== undefined && data[key] !== ''
):AnyObj => {
    if (typeof data === 'undefined' || !data) return {}
    return Object.keys(data)
        .filter(filterFn)
        .reduce((acc, key) => ({ ...acc, [key]: data[key] }), {})
}

/** 按照key移除指定 **/
export const removeObjectKey = ( data:AnyObj = {}, keyList:any[] | number | string = [] ) => {
    return Object.keys(data)
        .filter( key => (typeof keyList === "string" || typeof keyList === "number" )? key !== keyList : !keyList.some( ele => ele == key) )
        .reduce((acc:AnyObj, key:string) => ({ ...acc, [key]: data[key] }), {})
}
/** 类型校验 */
export const isType = (data:any,type?:string) => {
    //@ts-ignore
    const parseType = Object.prototype.toString.call(data).match(/\[object (.*?)\]/)[1].toLowerCase()
    return type ? parseType === type.toLowerCase() : parseType
}